<template>
    <div style="display:flex;justify-content:center;">
        <el-card style="width:50%">
            <el-form :model="form" label-width="80px" style="padding-right:30px" :rules="rules" ref="formRef">
                <div style="text-align:center;margin-bottom:30px;font-weight:bold;color:rgb(23, 236, 66)">会员充值</div>
                <el-form-item label="充值日期" prop="time">
                    <div class="block">
                        <el-date-picker
                            v-model="form.time"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="充值账号" prop="count">
                    <el-input v-model="form.count" id="count"></el-input>
                </el-form-item>
                <div style="text-align:center">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="update">充值</el-button>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default{
    name:"Recharge",
    data(){
        return {
            form:{},
            user:{},
            vip:{},
            rules:{
                time:[
                    {required:true,message:'请输入充值日期',trigger:'blur'}
                ],
                count:[
                    {required:true,message:'请输入充值账号',trigger:'blur'}
                ]
            },
        }
    },
    created(){

    },
    
    methods:{
        cancel(){
            this.form={
                time:'',
                count:'',
            }
            // 如果需要，清除验证错误信息
            this.$refs.formRef.resetFields();
        },
        update(){
            this.$refs.formRef.validate((valid) => {
                if (valid) {
                    this.vip.time=this.form.time
                    this.vip.count=this.form.count
                    this.$request.get('/user/selectByCount/'+this.form.count).then(res=>{
                        if(res.code==='200'){
                            if(res.data!=null){
                                this.user=res.data
                                    if(this.user.vip=="是"){
                                        this.$message.error("该账号已是VIP")
                                    }else{
                                        this.user.vip="是"
                                        this.$request.put('/user/update',this.user).then(res=>{
                                            if(res.code==='200'){
                                                this.$message.success('充值成功')
                                            }else{
                                                this.$message.error(res.msg)
                                            }
                                        })
                                        this.$request.post('/vip/add',this.vip).then(res=>{
                                        if(res.code==='200'){
                                            //this.$message.success('保存成功')
                                        }else{
                                            this.$message.error(res.msg)
                                        }
                                        })
                                    }
                                
                            }
                            else{
                                this.$message.error("该顾客账号不存在")
                                return
                            }
                        }else{
                            this.$message.error(res.msg)
                        }
                    })
                    this.form={
                        time:'',
                        count:''
                    }
                    this.user={}
                }
            })

        },

    }
}
</script>

<style scoped>

</style>